/*******************************************************************************
 * Copyright (c) 2011 TXT e-solutions SpA
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * This work was performed within the IoT_at_Work Project
 * and partially funded by the European Commission's
 * 7th Framework Programme under the research area ICT-2009.1.3
 * Internet of Things and enterprise environments.
 *
 *
 * Authors:
 *     Cristoforo Seccia (TXT e-solutions SpA)
 *
 * Contributors:
 *      Domenico Rotondi (TXT e-solutions SpA)
 *******************************************************************************/
package it.txt.access.capability.wizard.panel;

import it.txt.access.capability.factory.values.root.RootCapabilityAction;
import it.txt.access.capability.spi.resource.ICapabilityResourceID;
import it.txt.access.capability.spi.resource.exception.URISyntaxException;
import it.txt.access.capability.spi.resource.model.ICapabilityResourceIDModel;
import it.txt.access.capability.spi.resource.visual.ICapabilityResourceIDDialog;
import it.txt.access.capability.wizard.panel.controller.ControllerPanelAccessTypePolicy;
import it.txt.access.capability.wizard.panel.exception.PanelAccessTypePolicyException;
import java.awt.Frame;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Cristoforo Seccia (TXT e-solutions SpA)
 */
public class PanelAccessTypePolicy extends javax.swing.JPanel {

    private DateChooserDialog notBeforeDateDialog = null;
    private DateChooserDialog notAfterDateDialog = null;
    private ControllerPanelAccessTypePolicy controller;
    private Frame myContainer;

    /** Creates new form WelcomePanel */
    public PanelAccessTypePolicy(Frame container, ControllerPanelAccessTypePolicy cpatp) {
        initComponents();
        checkIssuerDocument();
        checkAccessRightsTableRows();
        checkNotBeforeDateDocument();
        checkNotBeforeTimeDocument();
        checkNotAfterDateDocument();
        checkNotAfterTimeDocument();
        controller = cpatp;
        myContainer = container;
        policyResourceIDTypeCombo.removeAllItems();
        notBeforeDateDialog = new DateChooserDialog(myContainer, true);
        notAfterDateDialog = new DateChooserDialog(myContainer, true);
        ArrayList<ICapabilityResourceID> resourceIDTypes = controller.getCapabilityResopurceIDTypes();
        for (ICapabilityResourceID iCapabilityResourceID : resourceIDTypes) {
            policyResourceIDTypeCombo.addItem(iCapabilityResourceID);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        centerPanel = new javax.swing.JPanel();
        centerTitlePanel = new javax.swing.JPanel();
        jSeparator1 = new javax.swing.JSeparator();
        welcomeLabel = new javax.swing.JLabel();
        policyIssuerLabel = new javax.swing.JLabel();
        policyAssigneeLabel = new javax.swing.JLabel();
        policyResourceIDLabel = new javax.swing.JLabel();
        policyIssuerText = new javax.swing.JTextField();
        policyAssigneeText = new javax.swing.JTextField();
        newCapabilityResourceIDButton = new javax.swing.JButton();
        policyRevocationSrvLabel = new javax.swing.JLabel();
        policyRevocationSrvRemoveButton = new javax.swing.JButton();
        policyRevocationSrvAddButton = new javax.swing.JButton();
        policyRevocationSrvComboBox = new javax.swing.JComboBox();
        policyAccessRightsPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        policyAccessRightsTable = new javax.swing.JTable();
        policyAccessRightsAdd = new javax.swing.JButton();
        policyAccessRightsCopy = new javax.swing.JButton();
        policyAccessRightsDelete = new javax.swing.JButton();
        policyValidityConditionPanel = new javax.swing.JPanel();
        poliocyValidityConditionNotBeforeLabel = new javax.swing.JLabel();
        poliocyValidityConditionNotAfterLabel = new javax.swing.JLabel();
        policyValidityConditionNotBeforeDateButton = new javax.swing.JButton();
        policyValidityConditionNotAfterDateButton = new javax.swing.JButton();
        policyValidityConditionNotBeforeDateText = new javax.swing.JFormattedTextField();
        policyValidityConditionNotAfterDateText = new javax.swing.JFormattedTextField();
        policyValidityConditionNotBeforeTimeText = new javax.swing.JFormattedTextField();
        policyValidityConditionNotAfterTimeText = new javax.swing.JFormattedTextField();
        policyResourceIDType = new javax.swing.JLabel();
        policyResourceIDTypeCombo = new javax.swing.JComboBox();
        policyResourceIDText = new javax.swing.JTextField();

        centerPanel.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));

        centerTitlePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("IoT@Work - Capability Based Authorization"));

        welcomeLabel.setText("Create Root Access Capability");

        policyIssuerLabel.setText("Issuer *");

        policyAssigneeLabel.setText("Assignee");

        policyResourceIDLabel.setText("Resource ID *");

        policyIssuerText.setToolTipText("Please insert the Delegation Issuer.");
        policyIssuerText.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));
        policyIssuerText.setMinimumSize(new java.awt.Dimension(6, 20));
        policyIssuerText.setPreferredSize(new java.awt.Dimension(6, 20));

        policyAssigneeText.setEditable(false);
        policyAssigneeText.setToolTipText("In this type of delegation the Issuer and the Assignee are the same.");

        newCapabilityResourceIDButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icons_16x16/Key_16x16.png"))); // NOI18N
        newCapabilityResourceIDButton.setText("Set");
        newCapabilityResourceIDButton.setToolTipText("Choose you resource from the File System.");
        newCapabilityResourceIDButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newCapabilityResourceIDButtonActionPerformed(evt);
            }
        });

        policyRevocationSrvLabel.setText("Revocation Services");

        policyRevocationSrvRemoveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icons_16x16/Remove_16x16.png"))); // NOI18N
        policyRevocationSrvRemoveButton.setText("Remove");
        policyRevocationSrvRemoveButton.setToolTipText("Remove the selected Revocation Service URI.");
        policyRevocationSrvRemoveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                policyRevocationSrvRemoveButtonActionPerformed(evt);
            }
        });

        policyRevocationSrvAddButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icons_16x16/Add_16x16.png"))); // NOI18N
        policyRevocationSrvAddButton.setText("Add");
        policyRevocationSrvAddButton.setToolTipText("Add Revocation Service URI.");
        policyRevocationSrvAddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                policyRevocationSrvAddButtonActionPerformed(evt);
            }
        });

        policyRevocationSrvComboBox.setEditable(true);
        policyRevocationSrvComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "https://www.txtgroup.com/Services/Iot@Work/RevocationServices/write_authorization", "https://www.txtgroup.com/Services/Iot@Work/RevocationServices/read_authorization" }));
        policyRevocationSrvComboBox.setToolTipText("You can add  ore remove a Revocation Service. At least one should be used.");

        policyAccessRightsPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Access Rights"));

        policyAccessRightsTable.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
        policyAccessRightsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"Create",  new Boolean(true)},
                {"Read",  new Boolean(true)},
                {"Update",  new Boolean(false)},
                {"Delete",  new Boolean(false)}
            },
            new String [] {
                "Permitted Action", "Delegable"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Boolean.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        policyAccessRightsTable.setCellSelectionEnabled(true);
        jScrollPane1.setViewportView(policyAccessRightsTable);

        policyAccessRightsAdd.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icons_16x16/Add_16x16.png"))); // NOI18N
        policyAccessRightsAdd.setText("Add");
        policyAccessRightsAdd.setToolTipText("Add a new Permitted Action.");
        policyAccessRightsAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                policyAccessRightsAddActionPerformed(evt);
            }
        });

        policyAccessRightsCopy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icons_16x16/Copy_16x16.png"))); // NOI18N
        policyAccessRightsCopy.setText("Copy");
        policyAccessRightsCopy.setToolTipText("Copy the selected Permitted Action as a new one.");
        policyAccessRightsCopy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                policyAccessRightsCopyActionPerformed(evt);
            }
        });

        policyAccessRightsDelete.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icons_16x16/Remove_16x16.png"))); // NOI18N
        policyAccessRightsDelete.setText("Remove");
        policyAccessRightsDelete.setToolTipText("Remove the selected Permitted Action.");
        policyAccessRightsDelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                policyAccessRightsDeleteActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout policyAccessRightsPanelLayout = new javax.swing.GroupLayout(policyAccessRightsPanel);
        policyAccessRightsPanel.setLayout(policyAccessRightsPanelLayout);
        policyAccessRightsPanelLayout.setHorizontalGroup(
            policyAccessRightsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, policyAccessRightsPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 386, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(policyAccessRightsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(policyAccessRightsDelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(policyAccessRightsCopy, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(policyAccessRightsAdd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        policyAccessRightsPanelLayout.setVerticalGroup(
            policyAccessRightsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(policyAccessRightsPanelLayout.createSequentialGroup()
                .addGroup(policyAccessRightsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(policyAccessRightsPanelLayout.createSequentialGroup()
                        .addComponent(policyAccessRightsAdd)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(policyAccessRightsCopy)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(policyAccessRightsDelete))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        policyValidityConditionPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Validity Condition"));

        poliocyValidityConditionNotBeforeLabel.setText("Not Before *");

        poliocyValidityConditionNotAfterLabel.setText("Not After *");

        policyValidityConditionNotBeforeDateButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icons_16x16/Calendar_16x16.png"))); // NOI18N
        policyValidityConditionNotBeforeDateButton.setText("Select");
        policyValidityConditionNotBeforeDateButton.setToolTipText("Select the Delegation 's start Date.");
        policyValidityConditionNotBeforeDateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                policyValidityConditionNotBeforeDateButtonActionPerformed(evt);
            }
        });

        policyValidityConditionNotAfterDateButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/icons_16x16/Calendar_16x16.png"))); // NOI18N
        policyValidityConditionNotAfterDateButton.setText("Select");
        policyValidityConditionNotAfterDateButton.setToolTipText("Select the Delegation 's end Date.");
        policyValidityConditionNotAfterDateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                policyValidityConditionNotAfterDateButtonActionPerformed(evt);
            }
        });

        policyValidityConditionNotBeforeDateText.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));
        policyValidityConditionNotBeforeDateText.setEditable(false);
        policyValidityConditionNotBeforeDateText.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG))));
        policyValidityConditionNotBeforeDateText.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        policyValidityConditionNotAfterDateText.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));
        policyValidityConditionNotAfterDateText.setEditable(false);
        policyValidityConditionNotAfterDateText.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(java.text.DateFormat.getDateInstance(java.text.DateFormat.LONG))));
        policyValidityConditionNotAfterDateText.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        policyValidityConditionNotBeforeTimeText.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));
        policyValidityConditionNotBeforeTimeText.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("HH:mm:ss.SSS Z"))));
        policyValidityConditionNotBeforeTimeText.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        policyValidityConditionNotAfterTimeText.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(255, 0, 0)));
        policyValidityConditionNotAfterTimeText.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("HH:mm:ss.SSS Z"))));
        policyValidityConditionNotAfterTimeText.setHorizontalAlignment(javax.swing.JTextField.RIGHT);

        javax.swing.GroupLayout policyValidityConditionPanelLayout = new javax.swing.GroupLayout(policyValidityConditionPanel);
        policyValidityConditionPanel.setLayout(policyValidityConditionPanelLayout);
        policyValidityConditionPanelLayout.setHorizontalGroup(
            policyValidityConditionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(policyValidityConditionPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(policyValidityConditionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(poliocyValidityConditionNotAfterLabel)
                    .addComponent(poliocyValidityConditionNotBeforeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(policyValidityConditionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(policyValidityConditionNotBeforeDateText, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE)
                    .addComponent(policyValidityConditionNotAfterDateText, javax.swing.GroupLayout.DEFAULT_SIZE, 188, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(policyValidityConditionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(policyValidityConditionNotBeforeDateButton)
                    .addComponent(policyValidityConditionNotAfterDateButton))
                .addGap(18, 18, 18)
                .addGroup(policyValidityConditionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(policyValidityConditionNotAfterTimeText)
                    .addComponent(policyValidityConditionNotBeforeTimeText, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        policyValidityConditionPanelLayout.setVerticalGroup(
            policyValidityConditionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(policyValidityConditionPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(policyValidityConditionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(policyValidityConditionNotBeforeDateButton)
                    .addComponent(poliocyValidityConditionNotBeforeLabel)
                    .addComponent(policyValidityConditionNotBeforeDateText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(policyValidityConditionNotBeforeTimeText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(policyValidityConditionPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(policyValidityConditionNotAfterDateButton)
                    .addComponent(poliocyValidityConditionNotAfterLabel)
                    .addComponent(policyValidityConditionNotAfterTimeText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(policyValidityConditionNotAfterDateText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        policyResourceIDType.setText("Resource ID Type");

        policyResourceIDText.setEditable(false);

        javax.swing.GroupLayout centerTitlePanelLayout = new javax.swing.GroupLayout(centerTitlePanel);
        centerTitlePanel.setLayout(centerTitlePanelLayout);
        centerTitlePanelLayout.setHorizontalGroup(
            centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(centerTitlePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(policyValidityConditionPanel, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(welcomeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 531, Short.MAX_VALUE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 531, Short.MAX_VALUE)
                    .addGroup(centerTitlePanelLayout.createSequentialGroup()
                        .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(policyIssuerLabel)
                            .addComponent(policyAssigneeLabel)
                            .addComponent(policyRevocationSrvLabel)
                            .addComponent(policyResourceIDType)
                            .addComponent(policyResourceIDLabel))
                        .addGap(18, 18, 18)
                        .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(centerTitlePanelLayout.createSequentialGroup()
                                .addComponent(policyResourceIDText, javax.swing.GroupLayout.PREFERRED_SIZE, 310, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(newCapabilityResourceIDButton, javax.swing.GroupLayout.DEFAULT_SIZE, 88, Short.MAX_VALUE))
                            .addComponent(policyAssigneeText, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
                            .addComponent(policyIssuerText, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 416, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, centerTitlePanelLayout.createSequentialGroup()
                                .addComponent(policyRevocationSrvComboBox, 0, 218, Short.MAX_VALUE)
                                .addGap(18, 18, 18)
                                .addComponent(policyRevocationSrvAddButton)
                                .addGap(18, 18, 18)
                                .addComponent(policyRevocationSrvRemoveButton, javax.swing.GroupLayout.PREFERRED_SIZE, 91, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(policyResourceIDTypeCombo, 0, 416, Short.MAX_VALUE)))
                    .addComponent(policyAccessRightsPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        centerTitlePanelLayout.setVerticalGroup(
            centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(centerTitlePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(welcomeLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(policyIssuerLabel)
                    .addComponent(policyIssuerText, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(policyAssigneeLabel)
                    .addComponent(policyAssigneeText, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(policyRevocationSrvLabel)
                    .addComponent(policyRevocationSrvComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(policyRevocationSrvRemoveButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(policyRevocationSrvAddButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(policyResourceIDType)
                    .addComponent(policyResourceIDTypeCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(centerTitlePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(policyResourceIDLabel)
                    .addComponent(newCapabilityResourceIDButton)
                    .addComponent(policyResourceIDText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(14, 14, 14)
                .addComponent(policyAccessRightsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(policyValidityConditionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );

        javax.swing.GroupLayout centerPanelLayout = new javax.swing.GroupLayout(centerPanel);
        centerPanel.setLayout(centerPanelLayout);
        centerPanelLayout.setHorizontalGroup(
            centerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(centerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(centerTitlePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        centerPanelLayout.setVerticalGroup(
            centerPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(centerPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(centerTitlePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(centerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(centerPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void policyValidityConditionNotBeforeDateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_policyValidityConditionNotBeforeDateButtonActionPerformed
        handlePolicyValidityConditionNotBeforeDateButtonActionPerformed();
    }//GEN-LAST:event_policyValidityConditionNotBeforeDateButtonActionPerformed

    private void policyValidityConditionNotAfterDateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_policyValidityConditionNotAfterDateButtonActionPerformed
        handlePolicyValidityConditionNotAfterDateButtonActionPerformed();
    }//GEN-LAST:event_policyValidityConditionNotAfterDateButtonActionPerformed

    private void policyAccessRightsAddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_policyAccessRightsAddActionPerformed
        handlePolicyAccessRightsAddActionPerformed();
    }//GEN-LAST:event_policyAccessRightsAddActionPerformed

    private void policyAccessRightsCopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_policyAccessRightsCopyActionPerformed
        handlePolicyAccessRightsCopyActionPerformed();
    }//GEN-LAST:event_policyAccessRightsCopyActionPerformed

    private void policyAccessRightsDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_policyAccessRightsDeleteActionPerformed
        handlePolicyAccessRightsDeleteActionPerformed();
    }//GEN-LAST:event_policyAccessRightsDeleteActionPerformed

    private void policyRevocationSrvAddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_policyRevocationSrvAddButtonActionPerformed
        handlePolicyRevocationSrvAddButtonActionPerformed();
    }//GEN-LAST:event_policyRevocationSrvAddButtonActionPerformed

    private void policyRevocationSrvRemoveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_policyRevocationSrvRemoveButtonActionPerformed
        handlePolicyRevocationSrvRemoveButtonActionPerformed();
    }//GEN-LAST:event_policyRevocationSrvRemoveButtonActionPerformed

    private void newCapabilityResourceIDButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newCapabilityResourceIDButtonActionPerformed
        handleNewCapabilityResourceIDButtonActionPerformed();
    }//GEN-LAST:event_newCapabilityResourceIDButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel centerPanel;
    private javax.swing.JPanel centerTitlePanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton newCapabilityResourceIDButton;
    private javax.swing.JButton policyAccessRightsAdd;
    private javax.swing.JButton policyAccessRightsCopy;
    private javax.swing.JButton policyAccessRightsDelete;
    private javax.swing.JPanel policyAccessRightsPanel;
    private javax.swing.JTable policyAccessRightsTable;
    private javax.swing.JLabel policyAssigneeLabel;
    private javax.swing.JTextField policyAssigneeText;
    private javax.swing.JLabel policyIssuerLabel;
    private javax.swing.JTextField policyIssuerText;
    private javax.swing.JLabel policyResourceIDLabel;
    private javax.swing.JTextField policyResourceIDText;
    private javax.swing.JLabel policyResourceIDType;
    private javax.swing.JComboBox policyResourceIDTypeCombo;
    private javax.swing.JButton policyRevocationSrvAddButton;
    private javax.swing.JComboBox policyRevocationSrvComboBox;
    private javax.swing.JLabel policyRevocationSrvLabel;
    private javax.swing.JButton policyRevocationSrvRemoveButton;
    private javax.swing.JButton policyValidityConditionNotAfterDateButton;
    private javax.swing.JFormattedTextField policyValidityConditionNotAfterDateText;
    private javax.swing.JFormattedTextField policyValidityConditionNotAfterTimeText;
    private javax.swing.JButton policyValidityConditionNotBeforeDateButton;
    private javax.swing.JFormattedTextField policyValidityConditionNotBeforeDateText;
    private javax.swing.JFormattedTextField policyValidityConditionNotBeforeTimeText;
    private javax.swing.JPanel policyValidityConditionPanel;
    private javax.swing.JLabel poliocyValidityConditionNotAfterLabel;
    private javax.swing.JLabel poliocyValidityConditionNotBeforeLabel;
    private javax.swing.JLabel welcomeLabel;
    // End of variables declaration//GEN-END:variables

    private void handlePolicyValidityConditionNotBeforeDateButtonActionPerformed() {
        notBeforeDateDialog.setLocationRelativeTo(myContainer);
        notBeforeDateDialog.setVisible(true);
        if (notBeforeDateDialog.getReturnStatus() == DateChooserDialog.RET_OK) {
            policyValidityConditionNotBeforeDateText.setValue(notBeforeDateDialog.getSelectedDate());
            policyValidityConditionNotBeforeTimeText.setValue(notBeforeDateDialog.getSelectedDate());
            notAfterDateDialog.setMinSelectableDate(notBeforeDateDialog.getSelectedDate());
        }
    }                                                                          

    private void handlePolicyValidityConditionNotAfterDateButtonActionPerformed() {
        notAfterDateDialog.setLocationRelativeTo(myContainer);
        notAfterDateDialog.setVisible(true);
        if (notAfterDateDialog.getReturnStatus() == DateChooserDialog.RET_OK) {
            policyValidityConditionNotAfterDateText.setValue(notAfterDateDialog.getSelectedDate());
            policyValidityConditionNotAfterTimeText.setValue(notAfterDateDialog.getSelectedDate());
            notBeforeDateDialog.setMaxSelectableDate(notAfterDateDialog.getSelectedDate());
        }
    }                                                                         

    private void handlePolicyAccessRightsAddActionPerformed() {
        policyAccessRightsCopy.setEnabled(true);
        policyAccessRightsDelete.setEnabled(true);
        DefaultTableModel model = (DefaultTableModel) policyAccessRightsTable.getModel();
        Object[] row = new Object[2];
        row[1] = Boolean.FALSE;
        model.addRow(row);
    }                                                     

    private void handlePolicyAccessRightsCopyActionPerformed() {
        DefaultTableModel model = (DefaultTableModel) policyAccessRightsTable.getModel();
        int selectedRow = policyAccessRightsTable.getSelectedRow();
        Vector row = (Vector) model.getDataVector().elementAt(selectedRow);
        Vector copy = (Vector) row.clone();
        model.addRow(copy);
    }                                                      

    private void handlePolicyAccessRightsDeleteActionPerformed() {
        DefaultTableModel model = (DefaultTableModel) policyAccessRightsTable.getModel();
        int selectedRow = policyAccessRightsTable.getSelectedRow();
        try {
            model.removeRow(selectedRow);
            if (policyAccessRightsTable.getRowCount() == 0) {
                policyAccessRightsCopy.setEnabled(false);
                policyAccessRightsDelete.setEnabled(false);
            }
        } catch (Exception e) {
            //Ignore
        }
    }                                                        

    private void handlePolicyRevocationSrvAddButtonActionPerformed() {
        String typedValue = (String) policyRevocationSrvComboBox.getSelectedItem();
        policyRevocationSrvComboBox.addItem(typedValue);
        policyRevocationSrvRemoveButton.setEnabled(true);
    }                                                            

    private void handlePolicyRevocationSrvRemoveButtonActionPerformed() {
        int selectedItem = policyRevocationSrvComboBox.getSelectedIndex();
        policyRevocationSrvComboBox.removeItemAt(selectedItem);
        if (selectedItem == 0) {
            policyRevocationSrvRemoveButton.setEnabled(false);
        }
    }

    private void checkIssuerDocument() {

        policyIssuerText.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                sorroundIssuerDocumentChange(e);
            }

            public void removeUpdate(DocumentEvent e) {
                sorroundIssuerDocumentChange(e);
            }

            public void changedUpdate(DocumentEvent e) {
                sorroundIssuerDocumentChange(e);
            }

            private void sorroundIssuerDocumentChange(DocumentEvent e) {
                String strIssuer = policyIssuerText.getText();
                policyAssigneeText.setText(strIssuer);
                if (!strIssuer.equals("")) {
                    policyIssuerText.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(0, 0, 0)));
                } else {
                    policyIssuerText.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(255, 0, 0)));
                }
            }
        });
    }

    private void checkAccessRightsTableRows() {

        policyAccessRightsTable.getModel().addTableModelListener(new TableModelListener() {

            public void tableChanged(TableModelEvent e) {

                if (e.getType() == TableModelEvent.INSERT) {
                    policyAccessRightsTable.setBorder(
                                javax.swing.BorderFactory.createLineBorder(
                                new java.awt.Color(255, 0, 0)));
                }
                else if (e.getType() == TableModelEvent.UPDATE) {
                    int row = e.getFirstRow();
                    String elem = (String) policyAccessRightsTable.getModel().getValueAt(row, 0);
                    if (elem != null && !elem.equals("")) {
                        policyAccessRightsTable.setBorder(
                                javax.swing.BorderFactory.createLineBorder(
                                new java.awt.Color(0, 0, 0)));
                    } else {
                        policyAccessRightsTable.setBorder(
                                javax.swing.BorderFactory.createLineBorder(
                                new java.awt.Color(255, 0, 0)));
                    }
                }
            }
        });
    }

    private void checkNotBeforeDateDocument() {

        policyValidityConditionNotBeforeDateText.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                changeNotAfterDateBorderColor(e);
            }

            public void removeUpdate(DocumentEvent e) {
                changeNotAfterDateBorderColor(e);
            }

            public void changedUpdate(DocumentEvent e) {
                changeNotAfterDateBorderColor(e);
            }

            private void changeNotAfterDateBorderColor(DocumentEvent e) {
                if (policyValidityConditionNotBeforeDateText.getValue() != null) {
                    policyValidityConditionNotBeforeDateText.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(0, 0, 0)));
                } else {
                    policyValidityConditionNotBeforeDateText.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(255, 0, 0)));
                }

            }
        });
    }

    private void checkNotBeforeTimeDocument() {

        policyValidityConditionNotBeforeTimeText.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                changeNotBeforeTimeBorderColor(e);
            }

            public void removeUpdate(DocumentEvent e) {
                changeNotBeforeTimeBorderColor(e);
            }

            public void changedUpdate(DocumentEvent e) {
                changeNotBeforeTimeBorderColor(e);
            }

            private void changeNotBeforeTimeBorderColor(DocumentEvent e) {
                if (policyValidityConditionNotBeforeTimeText.getValue() != null) {
                    policyValidityConditionNotBeforeTimeText.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(0, 0, 0)));
                } else {
                    policyValidityConditionNotBeforeTimeText.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(255, 0, 0)));
                }
            }
        });
    }

    private void checkNotAfterDateDocument() {

        policyValidityConditionNotAfterDateText.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                changeNotAfterDateBorderColor(e);
            }

            public void removeUpdate(DocumentEvent e) {
                changeNotAfterDateBorderColor(e);
            }

            public void changedUpdate(DocumentEvent e) {
                changeNotAfterDateBorderColor(e);
            }

            private void changeNotAfterDateBorderColor(DocumentEvent e) {
                if (policyValidityConditionNotAfterDateText.getValue() != null) {
                    policyValidityConditionNotAfterDateText.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(0, 0, 0)));
                } else {
                    policyValidityConditionNotAfterDateText.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(255, 0, 0)));
                }

            }
        });
    }

    private void checkNotAfterTimeDocument() {

        policyValidityConditionNotAfterTimeText.getDocument().addDocumentListener(new DocumentListener() {

            public void insertUpdate(DocumentEvent e) {
                changeNotAfterTimeBorderColor(e);
            }

            public void removeUpdate(DocumentEvent e) {
                changeNotAfterTimeBorderColor(e);
            }

            public void changedUpdate(DocumentEvent e) {
                changeNotAfterTimeBorderColor(e);
            }

            private void changeNotAfterTimeBorderColor(DocumentEvent e) {
                if (policyValidityConditionNotAfterTimeText.getValue() != null) {
                    policyValidityConditionNotAfterTimeText.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(0, 0, 0)));
                } else {
                    policyValidityConditionNotAfterTimeText.setBorder(
                            javax.swing.BorderFactory.createLineBorder(
                            new java.awt.Color(255, 0, 0)));
                }

            }
        });
    }

    private void handleNewCapabilityResourceIDButtonActionPerformed(){
        ICapabilityResourceID icrid = (ICapabilityResourceID) policyResourceIDTypeCombo.getSelectedItem();
        ICapabilityResourceIDDialog dialog = icrid.getDialog(myContainer, true);
        dialog.setLocationRelativeTo(myContainer);
        dialog.setVisible(true);
        if(dialog.getReturnStatus() == ICapabilityResourceIDDialog.RET_OK){
            ICapabilityResourceIDModel model = dialog.getModel();
            try {
                policyResourceIDText.setText(model.toURI().toString());
            } catch (URISyntaxException ex) {
                Logger.getLogger(PanelAccessTypePolicy.class.getName()).log(Level.SEVERE, ex.getMessage(), ex);
            }
        }
    }


    public boolean checkFormValidity() throws PanelAccessTypePolicyException {

        if (policyIssuerText.getText().equals("")) {
            throw new PanelAccessTypePolicyException("The Issuer value is empty!");
        }

        //At least one revocation service is available
        for (int i = 0; i < policyRevocationSrvComboBox.getModel().getSize(); i++) {
            String elem = (String) policyRevocationSrvComboBox.getModel().getElementAt(i);
            if (elem.equals("")) {
                throw new PanelAccessTypePolicyException("Invalid Revocation Services at index: " + (i + 1));
            }
        }

        if (policyResourceIDText.getText().equals("")) {
            throw new PanelAccessTypePolicyException("The Resource ID value is empty!");
        }

        //At least one Permitted action shoud be specified
        int rowCount = policyAccessRightsTable.getModel().getRowCount();
        if (rowCount == 0) {
            throw new PanelAccessTypePolicyException("No Permitted Actions specified!");
        }
        for (int i = 0; i < rowCount; i++) {
            String elem = (String) policyAccessRightsTable.getModel().getValueAt(i, 0);
            if (elem == null || elem.equals("")) {
                throw new PanelAccessTypePolicyException("Invalid Permitted Action at row: " + (i + 1));
            }
        }

        if (policyValidityConditionNotBeforeDateText.getValue() == null) {
            throw new PanelAccessTypePolicyException("The validity condition - not before date is empty or invalid!");
        }

        if (policyValidityConditionNotBeforeTimeText.getValue() == null) {
            throw new PanelAccessTypePolicyException("The validity condition - not before time is empty or invalid!");
        }

        if (policyValidityConditionNotAfterDateText.getValue() == null) {
            throw new PanelAccessTypePolicyException("The validity condition - not after date is empty or invalid!");
        }

        if (policyValidityConditionNotAfterTimeText.getValue() == null) {
            throw new PanelAccessTypePolicyException("The validity condition - not after time is empty or invalid!");
        }

        return true;
    }


    public String getPolicyIssuerText() {
        return policyIssuerText.getText();
    }
    
    public String getAssigneeText(){
        return policyAssigneeText.getText();
    }

    public List<String> getPolicyRevocationServices() {
        List<String> services = new ArrayList<String>();
        int size = policyRevocationSrvComboBox.getModel().getSize();
        for (int i = 0; i < size; i++) {
            String service = (String) policyRevocationSrvComboBox.getModel().getElementAt(i);
            services.add(service);
        }
        return services;
    }

    public String getPolicyResourceIDText() {
        return policyResourceIDText.getText();
    }

    public RootCapabilityAction[] getRootCapabilityActions() {
        DefaultTableModel model = (DefaultTableModel) policyAccessRightsTable.getModel();
        int rowCount = model.getRowCount();
        RootCapabilityAction[] actions = new RootCapabilityAction[rowCount];
        for (int row = 0; row < rowCount; row++) {
            RootCapabilityAction action = new RootCapabilityAction();
            action.setValue((String) model.getValueAt(row, 0));
            if(model.getValueAt(row, 1) == null){
                action.setDelegable(false);
            }
            else{
                action.setDelegable((Boolean) model.getValueAt(row, 1));
            }            
            actions[row] = action;
        }
        return actions;
    }

    public Date getNotBeforeDate() {
        return (Date)policyValidityConditionNotBeforeDateText.getValue();
    }

    public Date getNotBeforeTime() {
        return (Date)policyValidityConditionNotBeforeTimeText.getValue();
    }

    public Date getNotOrOnAfterDate() {
        return (Date)policyValidityConditionNotAfterDateText.getValue();
    }

    public Date getNotOrOnAfterTime() {
        return (Date)policyValidityConditionNotAfterTimeText.getValue();
    }

    public void setMinSelectableDate(Date minSelectableDate) {
        notBeforeDateDialog.setMinSelectableDate(minSelectableDate);
        notAfterDateDialog.setMinSelectableDate(minSelectableDate);
    }

    public void setMaxSelectableDate(Date maxSelectableDate) {
        notBeforeDateDialog.setMaxSelectableDate(maxSelectableDate);
        notAfterDateDialog.setMaxSelectableDate(maxSelectableDate);
    }
}
